using Godot;
using System.Collections.Generic;

public class BackPack : StoreView
{
    public Slot CraftSlot;
    public List<CraftSlot> craftSlots = new List<CraftSlot>();
    public Label DisplayName, Res;
    public Node2D CraftInfo;
    public BackPack() : base()
    {
        this.Title = "BackPack";
    }

    public override void _Ready()
    {
        base._Ready();
        ap = GetNode<AnimationPlayer>("AnimationPlayer");
        CraftInfo = GetNode<Node2D>("CraftInfo");
        DisplayName = GetNode<Label>("CraftInfo/DisplayName");
        Res = GetNode<Label>("CraftInfo/Res");
    }
    public override void Create()
    {
        base.Create();
        PackedScene ps = GD.Load<PackedScene>("res://Tscn/CraftSlot.tscn");
        for (int y = 0; y < 3; y++)
        {
            for (int x = 0; x < 9; x++)
            {
                CraftSlot cs = ps.Instance<CraftSlot>();
                cs.index=y*9+x;
                cs.player = player;
                cs.backPack = this;
                cs.SetPosition(new Vector2(3 + x * 21, -65 + y * 21));
                cs.MouseEnterEvent = MouseEnterEvent;
                cs.MouseExitEvent = MouseExitEvent;
                craftSlots.Add(cs);
                AddChild(cs);
            }
        }
    }
    public override void Updata(Player player)
    {
        base.Updata(player);
        if (player != null)
        {
            for (int i = 0; i < 27; i++)
            {
                craftSlots[i].SetFrame(2);
                craftSlots[i].SetRecipe(null);
            }
            int index = 0;
            foreach (Recipe r in player.store.recipes)
            {
                craftSlots[index].SetRecipe(r);
                craftSlots[index].SetFrame(0);
                index++;
            }
        }
    }

    public void MouseEnterEvent(CraftSlot craftSlot)
    {
        if (craftSlot.recipe != null)
        {
            CraftInfo.SetPosition(craftSlot.GetPosition());
            DisplayName.Visible = true;
            //Res.Visible = true;
            DisplayName.Text = craftSlot.recipe.Target[0].DispalyName + "\n" + craftSlot.recipe.getResList();
            //Res.Text = craftSlot.recipe.getResList();
        }
        else
        {
            DisplayName.Visible = false;
            //Res.Visible = false;
        }
    }
    public void MouseExitEvent(CraftSlot craftSlot)
    {
        DisplayName.Visible = false;
        //Res.Visible = false;
    }
}
